﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Obvious.Framework.Notification.Enums;
namespace Obvious.Framework.Notification.ConfigHandlers
{
    public class ObviousNotificationSection : ConfigurationSection
    {
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER, IsRequired = true)]
        public string DefaultProvider
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_DEFAULT_PROVIDER] = value;
            }
        }


        [ConfigurationProperty(Constants.C_CONFIG_COLLECTION_NOTIFICATION_PROVIDERS)]
        public NotificationProviderElementCollection NotificationProviders
        {
            get
            {
                return (NotificationProviderElementCollection)this[Constants.C_CONFIG_COLLECTION_NOTIFICATION_PROVIDERS];
            }
            set
            {
                this[Constants.C_CONFIG_COLLECTION_NOTIFICATION_PROVIDERS] = value;
            }
        }
    }

    public class NotificationProviderElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new NotificationProviderElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((NotificationProviderElement)element).Name;
        }
        public NotificationProviderElement this[int index]
        {
            get
            {
                return (NotificationProviderElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public NotificationProviderElement this[string Name]
        {
            get
            {
                return (NotificationProviderElement)BaseGet(Name);
            }
        }

    }

    public class NotificationProviderElementSettingsCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new NotificationProviderSettingsElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((NotificationProviderSettingsElement)element).Key;
        }
        public NotificationProviderSettingsElement this[int index]
        {
            get
            {
                return (NotificationProviderSettingsElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public NotificationProviderSettingsElement this[string Name]
        {
            get
            {
                return (NotificationProviderSettingsElement)BaseGet(Name);
            }
        }

    }

    public class NotificationProviderSettingsElement : ConfigurationElement
    {
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_KEY, IsRequired = true)]
        public string Key
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_KEY];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_KEY] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_VALUE, IsRequired = true)]
        public string Value
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_VALUE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_VALUE] = value;
            }
        }
    }

    public class NotificationProviderElement : ConfigurationElement
    {   
        [ConfigurationProperty(Constants.C_CONFIG_COLLECTION_SETTINGS)]
        public NotificationProviderElementSettingsCollection Settings
        {
            get
            {
                return (NotificationProviderElementSettingsCollection)this[Constants.C_CONFIG_COLLECTION_SETTINGS];
            }
            set
            {
                this[Constants.C_CONFIG_COLLECTION_SETTINGS] = value;
            }
        }

        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_TYPE,IsRequired = true)]
        public NotificationType Type
        {
            get
            {
                return (NotificationType)this[Constants.C_CONFIG_ATTRIBUTE_TYPE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_TYPE] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE,DefaultValue = "", IsRequired = false)]
        public string CustomType
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_CUSTOM_TYPE] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_NAME,IsRequired = true)]
        public string Name
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_NAME];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_NAME] = value;
            }
        }

        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_SMTP_HOST, DefaultValue = "127.0.0.1", IsRequired = false)]
        public string SmtpHost
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_SMTP_HOST];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_SMTP_HOST] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_SMTP_PORT, DefaultValue = "25", IsRequired = false)]
        [IntegerValidator()]
        public int SmtpPort
        {
            get
            {
                return (int)this[Constants.C_CONFIG_ATTRIBUTE_SMTP_PORT];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_SMTP_PORT] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_SMTP_USERNAME, DefaultValue = "", IsRequired = false)]
        public string SmtpUserName
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_SMTP_USERNAME];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_SMTP_USERNAME] = value;
            }
        }
        [ConfigurationProperty(Constants.C_CONFIG_ATTRIBUTE_SMTP_PWD, DefaultValue = "", IsRequired = false)]
        public string SmtpPwd
        {
            get
            {
                return (string)this[Constants.C_CONFIG_ATTRIBUTE_SMTP_PWD];
            }
            set
            {
                this[Constants.C_CONFIG_ATTRIBUTE_SMTP_PWD] = value;
            }
        }
    }
}
